const {
    getMyOrders,
    getOrdersById,
    updateOrder,
    delOrder,
    cancelOrder,
    getAllOrders,
    addOrder,//客户下单
} = require('../dao/ordersDao.js')

// 获取我负责的所有订单
module.exports.getMyOrders = async params => {
    const data = await getMyOrders(params);
    if (data.data.length > 0) {
        return {
            message: '获取成功',
            status: 1,
            data: data
        }
    } else {
        return {
            message: '获取失败',
            status: 0
        }
    }
}

// 获取修改订单数据
module.exports.getOrdersById = async params => {
    const data = await getOrdersById(params);
    console.log(data);
    if (data.length == 1) {
        return {
            message: '获取成功',
            status: 1,
            data
        }
    } else {
        return {
            message: '获取失败',
            status: 0
        }
    }
}

// 修改订单
module.exports.updateOrder = async params => {
    const data = await updateOrder(params);
    if (data.nModified > 0) {
        return {
            message: '修改成功',
            status: 1,
        }
    } else {
        return {
            message: '修改失败',
            status: 0
        }
    }
}

// 删除订单
module.exports.delOrder = async params => {
    const data = await delOrder(params);
    console.log(data);
    if (data.nModified > 0) {
        return {
            message: '删除成功',
            status: 1
        };
    } else {
        return {
            message: '删除失败',
            status: 0
        };
    }
}
// 退保申请
module.exports.cancelOrder = async params => {
    const data = await cancelOrder(params);
    if (data.nModified > 0) {
        return {
            message: '申请成功',
            status: 1
        };
    } else {
        return {
            message: '申请失败',
            status: 0
        };
    }
}

//管理员获取所有订单
module.exports.getAllOrders = async params => {
    try {
        const data = await getAllOrders(params);
        return {
            message: '获取成功',
            status: 1,
            ...data
        }
    } catch (error) {
        return {
            message: '获取失败,服务器出错',
            status: 0,
            error,
        }
    }
}

//客户下单
module.exports.addOrder = async params =>{
    const data = await addOrder(params);
    if(data._id){
        return {data,message:'成功',status:1}
    }else{
        return {data,message:'失败',status:0}
    }
}